package com.ddm.controller.admin;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ddm.constant.MessageConstant;
import com.ddm.dto.StandardDTO;
import com.ddm.entity.Standard;
import com.ddm.exception.DataErrorException;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.CommonService;
import com.ddm.service.LevelService;
import com.ddm.service.StandardService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标准表 前端控制器
 * </p>
 *
 * @author 丁丁猫
 * @since 2025-01-13
 */
@RestController("AdminStandardController")
@RequestMapping("/admin/standard")
@Slf4j
public class StandardController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private StandardService standardService;
    @Autowired
    private LevelService levelService;

    /**
     * 新增标准文档
     * @param standardDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增标准文档")
    public Result<String> add(@RequestBody StandardDTO standardDTO) {
        log.info("新增标准文档");
        Standard standard = new Standard();
        BeanUtils.copyProperties(standardDTO, standard);
        // 图片列表转字符串
        if (CollectionUtils.isNotEmpty(standardDTO.getImageList())) {
            String image = commonService.convertListToString(standardDTO.getImageList());
            standard.setImage(image);
        }
        // 保存
        standardService.save(standard);
        return Result.success();
    }
    /**
     * 新增标准文档
     * @param standardDTO
     * @return
     */
    @PostMapping("/plus")
    @ApiOperation("新增标准文档Plus")
    public Result<String> addPlus(@RequestBody StandardDTO standardDTO) {
        log.info("新增标准文档Plus");
        Standard standard = new Standard();
        BeanUtils.copyProperties(standardDTO, standard);
        // 图片列表转字符串
        if (CollectionUtils.isNotEmpty(standardDTO.getImageList())) {
            String image = commonService.convertListToString(standardDTO.getImageList());
            standard.setImage(image);
        }
        // 保存
        standardService.save(standard);
        levelService.savePlus(standard.getId());
        return Result.success();
    }

    /**
     * 根据id修改标准文档
     * @param standardDTO
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改标准文档")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<String> updateById(@RequestBody StandardDTO standardDTO) {
        log.info("根据id修改标准文档:{}", standardDTO.getId());
        // 判断是否存在
        if (null != standardService.getById(standardDTO.getId())) {
            Standard standard = new Standard();
            BeanUtils.copyProperties(standardDTO, standard);
            standard.setId(standardDTO.getId());
            // 图片列表转字符串
            if (!standardDTO.getImageList().isEmpty()) {
                String image = commonService.convertListToString(standardDTO.getImageList());
                standard.setImage(image);
            } else {
                standard.setImage("");
            }
            // 更新
            standardService.updateById(standard);
            return Result.success();
        } else {
            throw new DataErrorException(MessageConstant.NULL_RECORD);
        }
    }

    /**
     * 根据id删除标准文档
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除标准文档")
    public Result<String> deleteById(@PathVariable Long id) {
        log.info("根据id删除标准文档:{}", id);
        standardService.removeById(id);
        return Result.success();
    }

    /**
     * 获取标准菜单项
     * @return
     */
    @GetMapping("/menu")
    @ApiOperation("获取标准菜单项")
    public Result<PageResult> getMenu() {
        log.info("获取标准菜单项");
        return Result.success(standardService.pageQueryMenu());
    }
    /**
     * 批量删除标准文档
     * @param ids
     * @return
     */
    @DeleteMapping("/batch/{ids}")
    @ApiOperation("批量删除标准文档")
    public Result<String> batchDelete(@PathVariable List<Long> ids) {
        log.info("批量删除标准文档:{}", ids);
        standardService.removeByIds(ids);
        return Result.success();
    }
}

